# application/service/menu.py
from application.mapper.system.menu import MenuMapper
from typing import List, Dict, Any

class MenuService:
    """菜单业务逻辑层"""
    
    @staticmethod
    def get_menu_list(query_params: Dict[str, str]) -> List[Dict[str, Any]]:
        """
        获取菜单列表数据
        
        Args:
            query_params: 查询参数字典
                - parent_id: 父菜单ID
                - menu_type: 菜单类型
                
        Returns:
            List[Dict[str, Any]]: 菜单列表数据
        """
        try:
            # 验证和处理查询参数
            validated_params = MenuService._validate_query_params(query_params)
            
            # 调用数据访问层
            menu_list = MenuMapper.select_menu_list(validated_params)
            return menu_list
        except Exception as e:
            from application.extensions.logging import system_logger
            system_logger.error(f"获取菜单列表失败: {e}")
            raise e
        
    @staticmethod
    def _validate_query_params(query_params: Dict[str, str]) -> Dict[str, Any]:
        """
        验证和处理查询参数
        
        Args:
            query_params: 原始查询参数
            
        Returns:
            Dict[str, Any]: 验证后的查询参数
        """
        validated_params = {}
        
        # 验证 parentId 参数
        if query_params.get('parent_id'):
            try:
                validated_params['parent_id'] = int(query_params['parent_id'])
            except ValueError:
                # 如果 parentId 不是有效数字，则忽略该参数
                pass
        
        # 验证 menuType 参数
        if query_params.get('menu_type') in ['M', 'C', 'F']:
            validated_params['menu_type'] = query_params['menu_type']
            
        return validated_params